<?php

namespace App\Http\Middleware;

use App\Service\CommonService;
use Closure;
use Illuminate\Http\Request;

class LoginDetectionMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        if (empty($request->all()['token'])){
            return response()->json(['code'=>500,'msg'=>'用户未登录','data'=>[]]);
        }
        $uids=CommonService::verifyToken($request->all()['token']);
        if ($request->all()['uid'] != $uids){
            return response()->json(['code'=>500,'msg'=>'用户信息错误','data'=>[]]);
        }
        return $next($request);
    }
}
